package com.yunna.compose

import android.icu.text.Transliterator
import android.os.Bundle
import android.widget.TextView
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.viewModels
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.gestures.detectTapGestures
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxHeight
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.offset
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.requiredSizeIn
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.text.KeyboardActions
import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.foundation.verticalScroll
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.ArrowDropDown
import androidx.compose.material3.Button
import androidx.compose.material3.Checkbox
import androidx.compose.material3.Divider
import androidx.compose.material3.DropdownMenu
import androidx.compose.material3.DropdownMenuItem
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.ExposedDropdownMenuBox
import androidx.compose.material3.ExposedDropdownMenuDefaults
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.OutlinedTextField
import androidx.compose.material3.RadioButton
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.material3.TextField
import androidx.compose.material3.TextFieldDefaults
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.ExperimentalComposeUiApi
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.input.pointer.pointerInput
import androidx.compose.ui.layout.LayoutCoordinates
import androidx.compose.ui.layout.onGloballyPositioned
import androidx.compose.ui.platform.ComposeView
import androidx.compose.ui.platform.LocalSoftwareKeyboardController
import androidx.compose.ui.text.input.ImeAction
import androidx.compose.ui.unit.IntOffset
import androidx.compose.ui.unit.dp
import com.yunna.compose.ui.theme.ComposeTheme
import androidx.compose.ui.geometry.Rect
import androidx.compose.ui.layout.boundsInWindow
import kotlin.math.roundToInt

class MainActivity : ComponentActivity() {

    private val mViewModel by viewModels<MainViewModel>()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        /*setContent {
            ComposeTheme {
                LoginScreen(mViewModel)
            }
        }*/
        setContentView(R.layout.activity_main)

        val mComposeView = findViewById<ComposeView>(R.id.compose_view)

        mComposeView.setContent {
            /*ComposeTheme {
                LoginScreen(mViewModel)
            }*/
            LoginScreen(mViewModel)
        }


        findViewById<TextView>(R.id.test).setOnClickListener {
            mViewModel.test()
        }
    }
}


@OptIn(ExperimentalMaterial3Api::class, ExperimentalComposeUiApi::class)
@Composable
fun LoginScreen(myViewModel: MainViewModel) {
    val keyboardController = LocalSoftwareKeyboardController.current


    val collectAsState = myViewModel.number1.collectAsState()
    Surface(color = Color.White) {
        Column(
            modifier = Modifier
                .fillMaxSize()
                .padding(16.dp)
                .pointerInput(Unit) {
                    detectTapGestures {
                        keyboardController?.hide()
                    }
                },
            horizontalAlignment = Alignment.CenterHorizontally,
            verticalArrangement = Arrangement.Top
        ) {
            var username by remember { mutableStateOf("") }
            var password by remember { mutableStateOf("") }

            //AlignedSelectionBoxes()

            DropDown()


            TextField(
                value = username,
                onValueChange = { username = it },
                label = { Text("Username") },
                modifier = Modifier
                    .fillMaxWidth()
                    .padding(vertical = 8.dp),
                keyboardOptions = KeyboardOptions.Default.copy(imeAction = ImeAction.Next),
                keyboardActions = KeyboardActions(onNext = { /* Handle next action */ })
            )

            TextField(
                value = password,
                onValueChange = { password = it },
                label = { Text("Password") },
                modifier = Modifier
                    .fillMaxWidth()
                    .padding(vertical = 8.dp),
                keyboardOptions = KeyboardOptions.Default.copy(imeAction = ImeAction.Done),
                keyboardActions = KeyboardActions(onDone = {
                    myViewModel.performLogin(username, password)
                })
            )

            Button(
                onClick = {
                    myViewModel.performLogin(username, password)
                },
                modifier = Modifier
                    .fillMaxWidth()
                    .padding(vertical = 8.dp)
            ) {
                Text("${collectAsState.value}")
            }
        }
    }
}


@Composable
fun AlignedSelectionBoxes() {
    val dropdownExpanded = remember { mutableStateOf(false) }
    val selectValue = remember { mutableStateOf("default") }

    val options = listOf("Option 1", "Option 2", "Option 3", "Option 4", "Option 5")


    Column(modifier = Modifier.padding(16.dp)) {
        Button(
            onClick = {
                dropdownExpanded.value = !dropdownExpanded.value
            },
            modifier = Modifier
                .width(100.dp)
                .height(45.dp)
        ) {
            Text("${selectValue.value}")
        }


        // 创建一个简单的下拉菜单
        DropdownMenu(
            expanded = dropdownExpanded.value,
            onDismissRequest = { dropdownExpanded.value = false }
        ) {
            options.forEach {
                DropdownMenuItem(
                    onClick = {
                        selectValue.value = it
                        dropdownExpanded.value = !dropdownExpanded.value
                    },
                    text = { Text(it) }
                )
                Divider(color = Color.Red, thickness = 1.dp)
            }
        }
    }
}


@OptIn(ExperimentalMaterial3Api::class)
@Composable
private fun DropDown() {
    val dropDownEntries = (1..20)
    var isExpanded by remember { mutableStateOf(false) }
    val selectValue = remember { mutableStateOf("") }
    val scrollState = rememberScrollState()

    ExposedDropdownMenuBox(
        expanded = isExpanded,
        onExpandedChange = { isExpanded = it },
        modifier = Modifier
            .width(128.dp)
            .height(60.dp)
            .padding(0.dp),
    ) {
        OutlinedTextField(
            modifier = Modifier
                .menuAnchor()
                .fillMaxHeight()
                .fillMaxWidth(),
            value = selectValue.value,
            onValueChange = {
                isExpanded = true
            },
            label = { Text("Label") },
            //trailingIcon = { ExposedDropdownMenuDefaults.TrailingIcon(expanded = isExpanded) },
            readOnly = true,
            singleLine = true
        )
        ExposedDropdownMenu(
            modifier = Modifier
                .background(Color.White)
                .width(128.dp)
                .requiredSizeIn(maxHeight = 208.dp),
            expanded = isExpanded,
            onDismissRequest = { isExpanded = false },
        ) {
            dropDownEntries.forEach { entry ->
                Column(modifier = Modifier.padding(0.dp)) {
                    DropdownMenuItem(
                        modifier = Modifier.padding(0.dp),
                        text = { Text(text = entry.toString()) },
                        onClick = {
                            selectValue.value = entry.toString()
                            isExpanded = !isExpanded
                        },
                    )
                    Divider()
                }
            }
        }
    }
}

